package com.example.vv.model.login;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.InputType;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.vv.R;
import com.example.vv.fragmentWork.entity.LoginEntity;
import com.example.vv.fragmentWork.mvp.MvpActivity;
import com.example.vv.fragmentWork.utlis.InputTextHelper;
import com.example.vv.fragmentWork.utlis.ShareUtils;
import com.example.vv.fragmentWork.view.CustomerTitleBar;
import com.example.vv.model.login.base.contract.LoginContract;
import com.example.vv.model.login.base.presenter.LoginPresenter;
import com.example.vv.model.register.RegisterActivity;

import org.greenrobot.eventbus.EventBus;

import androidx.annotation.Nullable;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class LoginActivity extends MvpActivity<LoginContract.View, LoginPresenter> implements LoginContract.View {

    @BindView(R.id.username_et)
    EditText usernameEt;
    @BindView(R.id.pwd_et)
    EditText pwdEt;
    @BindView(R.id.eye_iv)
    ImageView eyeIv;
    @BindView(R.id.forgot_password)
    TextView forgotPassword;
    @BindView(R.id.register)
    TextView register;
    boolean flag = true;
    @BindView(R.id.login)
    TextView login;
    @BindView(R.id.login_title)
    CustomerTitleBar loginTitle;

    @Override
    public int initLayout() {
        return R.layout.activity_login;
    }

    @Override
    public LoginPresenter initPresenter() {
        return new LoginPresenter();
    }

    @Override
    public void initView() {
        InputTextHelper.with(this)
                .setMain(login)
                .addView(usernameEt)
                .addView(pwdEt)
                .setAlpha(true)
                .setListener(new InputTextHelper.OnInputTextListener() {
                    @Override
                    public boolean onInputChange(InputTextHelper helper) {
                        return presenter.isFormat();
                    }
                }).build();
        loginTitle.getLeft_img().setImageResource(R.mipmap.ic_back);
        loginTitle.getLeft_img().setOnClickListener((v)->{
            finish();
        });
        loginTitle.getCenter_tx().setText("登录");
    }

    @Override
    public void initData() {

    }

    @Override
    public void doBusiness() {

    }

    @Override
    public View titleView() {
        return null;
    }


    @Override
    public Context getMContext() {
        return this;
    }

    @Override
    public String getUserName() {
        return usernameEt.getText().toString().trim();
    }

    @Override
    public String getPassWord() {
        return pwdEt.getText().toString().trim();
    }

    @Override
    public void getMsg(String msg) {
        if (msg.equals("")) {
            return;
        }else {
            Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void setLoginEntity(LoginEntity loginEntity) {
        if (loginEntity != null) {
            ShareUtils.addUserInfo(this, loginEntity);
            EventBus.getDefault().postSticky(loginEntity);
            finish();
            Toast.makeText(this, "登录成功", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(this, "登录失败", Toast.LENGTH_SHORT).show();
        }
    }

    @OnClick({R.id.eye_iv, R.id.forgot_password, R.id.register, R.id.login})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.eye_iv:
                if (flag) {
                    eyeIv.setImageResource(R.mipmap.ic_eye_open);
                    pwdEt.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                    flag = false;
                } else {
                    eyeIv.setImageResource(R.mipmap.ic_eye_close);
                    pwdEt.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                    flag = true;
                }

                break;
            case R.id.forgot_password:
                break;
            case R.id.register:
                startActivityForResult(new Intent(LoginActivity.this, RegisterActivity.class),1);
                break;
            case R.id.login:
                presenter.login();
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == 99) {
            if (data.getStringExtra("username")!=null) {
                usernameEt.setText(data.getStringExtra("username"));
            }
        }
    }
}